package dao

import (
	"context"
	"mall-portalv2/internal/model"
	"time"
)

func (d *Dao) CreateCart(ctx context.Context, insertModel model.OmsCartItem) (model.OmsCartItem, error) {
	tb := model.OmsCartItem{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Create(&insertModel).Error; err != nil {
		return tb, err
	}
	return tb, nil
}

func (d *Dao) ClearCart(ctx context.Context, userId int64) error {
	tb := model.OmsCartItem{}
	updates := map[string]interface{}{
		"delete_status": 1,
		"modify_date":   time.Now(),
	}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ?", userId).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) DeleteCartWhereId(ctx context.Context, userId int64, id int64) error {
	tb := model.OmsCartItem{}
	updates := map[string]interface{}{
		"delete_status": 1,
		"modify_date":   time.Now(),
	}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ? AND id = ?", userId, id).Updates(updates).Error; err != nil {
		return err
	}
	return nil
}

func (d *Dao) QueryAllCart(ctx context.Context, userId int64) ([]model.OmsCartItem, error) {
	tb := model.OmsCartItem{}
	result := []model.OmsCartItem{}
	if err := d.db.WithContext(ctx).Table(tb.TableName()).Where("member_id = ? AND delete_status = 0", userId).Find(&result).Error; err != nil {
		return result, err
	}
	return result, nil
}
